package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.User;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.param.UserLoginParam;
import com.woniuxy.param.UserParam;
import com.woniuxy.service.UserService;
import com.woniuxy.util.JWTUtil;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //传入 user数据正常进行注册
    //传入 tel 自动生成account 和 psw
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public JsonResultDto register(UserParam userParam) {
        if(userParam==null){
            return new JsonResultDto("0001","注册失败请输入注册信息",null,null);
        }else{
            //userParam有数值的情况
            //只有电话号码传进来的情况自行生成account psw 进行注册
            User user = new User();
            if(userParam.getAccount()==null||userParam.getPsw()==null){
                    //通过验证码验证
                    //随机生成账号密码
                    String psw = "";
                    String account = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
                    for(int i=0;i<6;i++){
                        psw += String.valueOf((int)(Math.random()*10));
                    }
                    userParam.setAccount(account);
                    userParam.setPsw(psw);

                    BeanUtils.copyProperties(userParam,user);
                    this.save(user);
                    return new JsonResultDto("0000","注册成功",null,null);

            }else{
                BeanUtils.copyProperties(userParam,user);
                this.save(user);
                return new JsonResultDto("0000","注册成功",null,null);
            }
        }
    }
    public JsonResultDto getRegisterCode(String tel){
        if(tel!=null){
            StringBuffer code = new StringBuffer();
            for(int i=0;i<6;i++){
                code.append(String.valueOf((int)(Math.random()*10)));
            }
            redisConfig.opsForValue().set("woniuxy:"+tel,code.toString(),1000, TimeUnit.SECONDS);
            return new JsonResultDto("0003","验证码发送",code.toString(),null);
        }else{
            return new JsonResultDto("0004","请输入手机号",null,null);
        }

    }
    public JsonResultDto login(UserLoginParam userLoginParam){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("tel",userLoginParam.getTel());
        wrapper.eq("psw",userLoginParam.getPsw());
        User user = this.getOne(wrapper);
        String token = JWTUtil.generatorJWT(String.valueOf(user.getuId()));
        if(null!=user){
            return new JsonResultDto("0008","登陆成功!",token,null);
        }else{
            return new JsonResultDto("0009","登陆失败!",null,null);
        }
    }

    public void getCodeRocket(String tel){
        rocketMQTemplate.asyncSend("woniu:code", tel, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("调用成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("调用失败");
            }
        });
    }

}
